/**
 * Created by win7 on 2017/3/16.
 */
import React from "react";
import {Checkbox} from "antd";
import "./css/CompanyManage.css";
import CheckboxPick from "./CheckboxPick";
import GetEnterpriseListMutation from "./mutation/GetEnterpriseListMutation";
import UpdateEnterpriseStatusMutation from "./mutation/UpdateEnterpriseStatusMutation";
import Relay from "react-relay";
import Pagination from "./Pagination";
import ReactKey from "../../util/ReactKey";
import Util from "../../util/Util";


export default class CompanyDetail extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            forbid: false,
            pagenumber: '',
            datanumber: '',
            text: '',
            companyArr: [],
            pagesize: 15,
            enterpriseId: '',
            ischeckde: false,
            enterpriseName: ''
        }
        this.checkArr = []
    }

    //弹窗功能的操作
    onmodal(context, id) {
        Relay.Store.commitUpdate(
            new UpdateEnterpriseStatusMutation({
                //放传给后台参数的地方
                enterpriseIds: context.enterpriseId,
                status: context.status == 1 ? 0 : 1,
                reason: 'i'
            }),
            {
                onFailure: () => {
                    console.log(11110000)
                },
                onSuccess: (response) => {
                    let enterpriseName = this.state.enterpriseName;
                    let pageNum = this.refs.pagination.state.text;
                    this.onSure(10, pageNum,enterpriseName);
                }
            });
        // if (this.refs[id + 'forbid'].state.forbidstate) {
        //     this.refs.usecompany.setState({
        //         visible: true,
        //         companyName: context.enterpriseName,
        //         companyId: context.enterpriseId,
        //         status: context.status,
        //     })
        //     this.enterpriseId = context.enterpriseId
        // } else {
        //     this.refs.usecompany.setState({
        //         visible: true,
        //         companyName: context.enterpriseName,
        //         companyId: context.enterpriseId,
        //         status: context.status,
        //     })
        //     this.enterpriseId = context.enterpriseId
        // }
        // this.refs[id + 'forbid'].onchoose()
    }

    onSure(size, num, enerpriseName) {
        this.serchenterprize(num, size, enerpriseName)
    }

    serchenterprize(pageNum, pageSize, enerpriseName) {
        Relay.Store.commitUpdate(
            new GetEnterpriseListMutation({
                //放传给后台参数的地方
                pageNum: pageNum,
                pageSize: pageSize,
                enterpriseName: this.props.enterpriseName || ''
            }),
            {
                onFailure: () => {
                },
                onSuccess: (response) => {
                    let data = response.getEnterpriseList.data;
                    this.setState({
                        companyArr: data.enterpriseList,
                        totalresult: data.totalResult
                    })

                }
            });
    }

    //提交的功能
    onforbidCompany() {


    }

    onuseCompany(text) {
        // console.warn(text);
        // console.log(this.enterpriseId)
        // console.log(this.status)
        Relay.Store.commitUpdate(
            new UpdateEnterpriseStatusMutation({
                //放传给后台参数的地方
                enterpriseIds: this.enterpriseId,
                reason: text,
                status: this.status
            }),
            {
                onFailure: () => {
                    console.log(11110000)
                },
                onSuccess: (response) => {
                    let data = response.updateEnterpriseStatus.data;
                    //console.log(data.value)
                    //console.log(data.enterpriseList)
                }
            });
        this.refs.usecompany.setState({
            visible: false
        })
    }

    onmuchforbid() {
        this.refs.muchforbidcompany.setState({
            visible: true
        })
        this.checkArr = [];
        this.state.companyArr.map((context, index) => {
            //console.log(this.refs[index+'forbid'].state.selectsingle)
            if (this.refs[index + 'forbid'].state.selectsingle) {
                this.checkArr.push(context.enterpriseId)
            }
        });
        this.refs.muchforbidcompany.setState({
            companyNumber: this.checkArr.length
        })
        this.refs.forbidcompany.setState({
            visible: false
        })
    }

    onmuchuse() {
        //批量应用的复选框出现
        this.refs.muchusecompany.setState({
            visible: true
        })
        //批量禁用的功能实现
        this.checkArr = [];
        this.state.companyArr.map((context, index) => {
            //console.log(this.refs[index+'forbid'].state.selectsingle)
            if (this.refs[index + 'forbid'].state.selectsingle) {
                this.checkArr.push(context.enterpriseId)
            }
        });
        this.refs.muchusecompany.setState({
            companyNumber: this.checkArr.length
        })
        this.refs.usecompany.setState({
            visible: false
        })

    }

    onmuchuseData(text) {
        //提交checkArr即可
        //console.log(text)
        //console.log(this.checkArr.join(','))
        //提交之后弹窗消失
        this.refs.muchusecompany.setState({
            visible: false
        })
        //批量启用数据向后台传递参数
        Relay.Store.commitUpdate(
            new UpdateEnterpriseStatusMutation({
                //放传给后台参数的地方
                enterpriseIds: this.checkArr.join(','),
                reason: text,
                status: '1'
            }),
            {
                onFailure: () => {
                    console.log(11110000)
                },
                onSuccess: (response) => {
                    let data = response.updateEnterpriseStatus.data;
                    console.log(data.value)
                    //console.log(data.enterpriseList)
                }
            });

    }

    onmuchforbidData(text) {
        this.refs.muchforbidcompany.setState({
            visible: false
        })
        console.log(text)
        console.log(this.checkArr.join(','))
        Relay.Store.commitUpdate(
            new UpdateEnterpriseStatusMutation({
                //放传给后台参数的地方
                enterpriseIds: this.checkArr.join(','),
                reason: text,
                status: '0'
            }),
            {
                onFailure: () => {
                    console.log(11110000)
                },
                onSuccess: (response) => {
                    let data = response.updateEnterpriseStatus.data;
                    console.log(data)
                    //console.log(data.enterpriseList)
                }
            });
    }

    //点击跳转到的页码
    changePage() {
        console.log(this.state.text)
        Relay.Store.commitUpdate(
            new GetEnterpriseListMutation({
                //放传给后台参数的地方
                pageNum: this.state.text,
                pageSize: 10
            }),
            {
                onFailure: () => {
                    console.log(11110000)
                },
                onSuccess: (response) => {
                    let data = response.getEnterpriseList.data;
                    this.setState({
                        companyArr: data.enterpriseList
                    })
                }
            });
        this.refs.pagechange.value = this.state.text
    }

    changetext(e) {
        var pagecode = Math.floor(e.target.value)

        if (!/^[0-9]{1,3}$/.test(pagecode)) {
            return;
        }
        this.setState({
            text: pagecode
        });
    }

    checkischose() {
        this.setState({
            ischeckde: true
        });
        this.int = 0
    }

    checkisunchose() {
        this.setState({
            ischeckde: false
        });
        this.int = 1
    }

    allCheck(e) {
        // this.checkArr = [];
        // this.state.companyArr.map((context, index) => {
        //     this.refs[index + 'forbid'].setState({
        //         selectsingle: true
        //     })
        //     this.checkArr.push(context.enterpriseId)
        // });
        // console.log(this.checkArr)
        let is = e.target.checked;
        if (is) {
            this.setState({
                ischeckde: true,
                int: 0
            });
            this.int = 0
        } else {
            this.setState({
                ischeckde: false,
                int: 1
            });
            this.int = 1
        }
        this.isallclick(this.uid);
    }

    render() {
        return (
            <div className="company_content">
                <table className="company_content_table settle_rec_table">
                    <thead>
                    <tr style={{backgroundColor: '#f2f2f2'}}>
                        <th >企业ID</th>
                        <th >企业名称</th>
                        <th >类型</th>
                        <th >员工数</th>
                        <th >管理员</th>
                        <th >创建时间</th>
                        {/*<span className="comManage_title7">状态</span>*/}
                        <th style={!Util.authorityCheck("company") ? {display: 'none'} :{}}>
                            <span>操作</span>
                        </th>
                    </tr>
                    </thead>
                  <tbody>
                      {
                          this.state.companyArr.map((ele, index) => {
                              return <CheckboxPick
                                  context={ele} key={ReactKey.key}
                                  indexnumber={index}
                                  onmodal={this.onmodal.bind(this)}
                                  ref={index + 'forbid'}
                              >
                              </CheckboxPick>
                          })
                      }
                  </tbody>

                </table>
                {/*<div className="comManage_muchForbid">*/}
                {/*<Confirm tcontext={'+批量禁用'} touch={this.onmuchforbid.bind(this)}></Confirm>*/}
                {/*<Confirm tcontext={'+批量启用'} style={{marginLeft: '20px'}}*/}
                {/*touch={this.onmuchuse.bind(this)}></Confirm>*/}
                {/*</div>*/}
                {/*页码*/}
                <Pagination eachSize={10} dataSize={this.state.totalresult} onSure={this.onSure.bind(this)}
                            ref={'pagination'}/>

                {/*<ForbidComapny ref={'forbidcompany'} forbidCompany={this.onforbidCompany.bind(this)}></ForbidComapny>*/}
                {/*<UserCompany ref={'usecompany'} onuse={this.onuseCompany.bind(this)}></UserCompany>*/}
                {/*<MuchForbidCompany ref={'muchforbidcompany'}*/}
                {/*onmuchforbidData={this.onmuchforbidData.bind(this)}></MuchForbidCompany>*/}
                {/*<MuchUseComapny ref={'muchusecompany'} onmuchuseData={this.onmuchuseData.bind(this)}></MuchUseComapny>*/}
            </div>
        )
    }
}